<template>
  <div class="buy-drug flex flex-center">
    <div class="buy-drug-icon">
      <div class="iconfont iconyiyuan" />
    </div>
    <div class="flex1">
      <div class="buy-drug-title">
        {{ drugData.medicationTitle }}
      </div>
      <div class="buy-drug-sub-title">
        {{ drugData.medicationDescription }}
      </div>
    </div>
    <div class="buy-btn">
      立即进入
    </div>

    <template v-if="drugData.jumpType == 1">
      <wx-open-launch-weapp
        id="launch-btn"
        style="width: 100%;height: 100%;position: absolute;top: 0;left: 0;z-index: 999;"
        :path="drugData.appletsUrl"
        :username="drugData.appletsId"
      >
        <div v-is="'script'" type="text/wxtag-template">
          <div style="width:100%; height:100%; position: absolute;top:0;bottom:0;left:0;right:0;z-index:5;opacity: 0;" />
        </div>
      </wx-open-launch-weapp>
    </template>
    <template v-else>
      <div @click="openWeb" style="width:100%; height:100%; position: absolute;top:0;bottom:0;left:0;right:0;z-index:5;opacity: 0;" />
    </template>
  </div>  
</template>

<script>
import { nextTick, onMounted } from 'vue';
import XltData from '@/utils/XltData';
import { useUserInfo } from '@/hooks/store';
export default {
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ['onClick'],
  setup(props, { emit }) {
    const userInfo = useUserInfo();

    const openWeb = () => {
      sendData();
      setTimeout(() => {
        window.open(props.data.webUrl);
      });
    };

    const sendData = () => {
      let form = {
        medicationDescription: props.data.medicationDescription,
        medicationTitle: props.data.medicationTitle,
        companyName: props.data.companyName,
        webUrl: props.data.webUrl,
        appletsUrl: props.data.appletsUrl,
        appletsId: props.data.appletsId,
        title: props.data.title,
        articleId: props.data.articleId,
        patientsId: userInfo.value.patientsId //用户id
      };
      XltData.track('ArticleSkip', form);
      emit('onClick');
    };

    const listenerBtn = () => {
      let btn = document.getElementById('launch-btn');
      btn &&
        btn.addEventListener('launch', function () {
          sendData();
        });
    };

    onMounted(() => {
      nextTick(() => {
        listenerBtn();
      });
    });

    return {
      drugData: props.data,
      openWeb,
      sendData
    };
  }
};
</script>

<style lang="scss" scoped>
.buy-drug {
  padding: 26px;
  width: 100%;
  background: linear-gradient(180deg, #fdf7ea 0%, #fcdfc9 100%);
  border-radius: 8px;
  position: relative;

  &-icon {
    align-self: self-start;
    font-size: 36px;
    color: #ff6331;
    margin-right: 10px;
  }

  &-title {
    font-size: 36px;
    font-weight: 500;
    color: #111111;
    line-height: 50px;
    margin-bottom: 6px;
  }

  &-sub-title {
    font-size: 26px;
    font-weight: 400;
    color: #373d52;
    line-height: 36px;
  }
}

.buy-btn {
  width: 188px;
  height: 64px;
  background: linear-gradient(136deg, #e2ac73 0%, #cc9455 100%);
  box-shadow: 0px 4px 16px 0px rgba(242, 151, 24, 0.4);
  border-radius: 32px;
  font-size: 26px;
  font-weight: 500;
  color: #ffffff;
  line-height: 64px;
  text-align: center;
}
</style>